﻿using System;

namespace AladdinGameFramework.ObjectPool
{
	/// <summary>
	/// 对象基类
	/// </summary>
	public abstract class ObjectBase
	{
		private readonly string m_Name;
		private readonly object m_Target;
		private bool m_Locked;
		private int m_Priority;
		private DateTime m_LastUseTime;

		/// <summary>
		/// 初始化对象新实例
		/// </summary>
		/// <param name="target"></param>
		public ObjectBase(object target)
		:this(null, target, false, 0)
		{

		}

		/// <summary>
		/// 初始化对象新实例
		/// </summary>
		/// <param name="name">对象名称</param>
		/// <param name="target">对象</param>
		public ObjectBase(string name, object target)
			:this(name, target, false, 0)
		{

		}

		/// <summary>
		/// 初始化对象新实例
		/// </summary>
		/// <param name="name">对象名称</param>
		/// <param name="target">对象</param>
		/// <param name="priority">对象优先级</param>
		public ObjectBase(string name, object target, int priority)
			:this(name, target, false, priority)
		{

		}

		/// <summary>
		/// 初始化对象新实例
		/// </summary>
		/// <param name="name">对象名称</param>
		/// <param name="target">对象</param>
		/// <param name="locked">对象是否被锁定</param>
		/// <param name="priority">对象的优先级</param>
		public ObjectBase(string name, object target, bool locked, int priority)
		{
			if (target == null)
				throw new GameFrameworkException(string.Format("Target '{0}' is invalid.", name));

			m_Name = name ?? string.Empty;
			m_Target = target;
			m_Locked = locked;
			m_Priority = priority;
			m_LastUseTime = DateTime.Now;
		}

		/// <summary>
		/// 获取对象名称
		/// </summary>
		public string Name
		{
			get
			{
				return m_Name;
			}
		}

		/// <summary>
		/// 获取对象
		/// </summary>
		public object Target
		{
			get
			{
				return m_Target;
			}
		}

		/// <summary>
		/// 获取对象是否被加锁
		/// </summary>
		public bool Locked
		{
			get
			{
				return m_Locked;
			}
			set
			{
				m_Locked = value;
			}
		}

		public int Priority
		{
			get
			{
				return m_Priority;
			}
			set
			{
				m_Priority = value;
			}
		}

		/// <summary>
		/// 获取对象上次使用时间
		/// </summary>
		public DateTime LastUseTime
		{
			get
			{
				return m_LastUseTime;
			}
			internal set
			{
				m_LastUseTime = value;
			}
		}

		/// <summary>
		/// 获取对象时的事件
		/// </summary>
		protected internal virtual void OnSpawn()
		{

		}

		/// <summary>
		/// 回收对象时的事件
		/// </summary>
		protected internal virtual void OnUnspawn()
		{

		}

		/// <summary>
		/// 释放对象
		/// </summary>
		protected internal abstract void Release();

	}
}
